1   /*
2    * Copyright (C) 2012 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  
15  package com.google.common.io;
16  
17  import static com.google.common.base.Preconditions.checkNotNull;
18  
19  import com.google.common.annotations.GwtCompatible;
20  
21  import java.io.IOException;
22  
23  /**
24   * Provides simple GWT-compatible substitutes for {@code InputStream}, {@code OutputStream},
25   * {@code Reader}, and {@code Writer} so that {@code BaseEncoding} can use streaming implementations
26   * while remaining GWT-compatible.
27   *
28   * @author Louis Wasserman
29   */
30  @GwtCompatible(emulated = true)
31  final class GwtWorkarounds {
32    private GwtWorkarounds() {}
33  
34    /**
35     * A GWT-compatible substitute for a {@code Reader}.
36     */
37    interface CharInput {
38      int read() throws IOException;
39      void close() throws IOException;
40    }
41  
42    /**
43     * Views a {@code CharSequence} as a {@code CharInput}.
44     */
45    static CharInput asCharInput(final CharSequence chars) {
46      checkNotNull(chars);
47      return new CharInput() {
48        int index = 0;
49  
50        @Override
51        public int read() {
52          if (index < chars.length()) {
53            return chars.charAt(index++);
54          } else {
55            return -1;
56          }
57        }
58  
59        @Override
60        public void close() {
61          index = chars.length();
62        }
63      };
64    }
65  
66    /**
67     * A GWT-compatible substitute for an {@code InputStream}.
68     */
69    interface ByteInput {
70      int read() throws IOException;
71      void close() throws IOException;
72    }
73  
74    /**
75     * A GWT-compatible substitute for an {@code OutputStream}.
76     */
77    interface ByteOutput {
78      void write(byte b) throws IOException;
79      void flush() throws IOException;
80      void close() throws IOException;
81    }
82  
83    /**
84     * A GWT-compatible substitute for a {@code Writer}.
85     */
86    interface CharOutput {
87      void write(char c) throws IOException;
88      void flush() throws IOException;
89      void close() throws IOException;
90    }
91  
92    /**
93     * Returns a {@code CharOutput} whose {@code toString()} method can be used
94     * to get the combined output.
95     */
96    static CharOutput stringBuilderOutput(int initialSize) {
97      final StringBuilder builder = new StringBuilder(initialSize);
98      return new CharOutput() {
99  
100       @Override
101       public void write(char c) {
102         builder.append(c);
103       }
104 
105       @Override
106       public void flush() {}
107 
108       @Override
109       public void close() {}
110 
111       @Override
112       public String toString() {
113         return builder.toString();
114       }
115     };
116   }
117 }
118